package com.efast.dmom.mes.datahandlers.entity;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.bean.Station;
import com.efast.dmom.mes.dao.entity.StationDao;
import com.efast.dmom.mes.datahandlers.base.PlantModelDataHandler;
import com.efast.dmom.mes.entity.StationEntity;
import com.efast.dmom.mes.util.BaseEntityDataUtil;
import com.efast.dmom.mes.util.TimeUtil;

@Component
public class StationDataHandler extends PlantModelDataHandler<Station>{
	
	@Autowired
	private StationDao stationDao;
	
	@Override
	public ResultBean save(Station obj) {
		ResultBean resultBean = new ResultBean();
		if(obj==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		StationEntity innerData = obj.getInnerData();
		if(innerData==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater innerdata is null");
			return resultBean;
		}
		BaseEntityDataUtil.setBaseEntityData(innerData);
		int save = stationDao.save(innerData);
		if(save>0) {
			resultBean.setSuccess(true);
		}else {
			resultBean.setSuccess(false);
			resultBean.setMessage("insert fail");
		}
		resultBean.setObject(obj);
		return resultBean;
	}
	
	@Override
	public ResultBean update(Station obj) {
		ResultBean resultBean = new ResultBean();
		if (obj.getInnerData() != null) {

			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());

			stationDao.update(obj.getInnerData());
		}
		return resultBean;
	}
	
	@Override
	public ResultBean delete(Station obj) {
		ResultBean resultBean = new ResultBean();
		if (obj.getInnerData() != null) {

			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());

			stationDao.delete(obj.getInnerData());
		}
		return resultBean;
	}

	public Station getById(long id) {
		Station station = new Station();
		station.setInnerData(stationDao.getById(id));
		return station;
	}

	public Station getByName(String name) {
		Station station = new Station();
		station.setInnerData(stationDao.getByName(name));
		return station;
	}


	public Station getByCode(String StationCode) {
		Station station = new Station();
		station.setInnerData(stationDao.getByCode(StationCode));
		return station;
	}

	@Override
	public ResultBean saveBatch(List<Station> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean updateBatch(List<Station> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean deleteBatch(List<Station> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	


}
